package com.kfm.servlet;

import com.kfm.model.Account;
import com.kfm.service.AccountService;
import com.kfm.util.Contains;
import com.wf.captcha.utils.CaptchaUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cookie[] cookies = req.getCookies();
        String username = null;
        String password = null;
        if (cookies != null){
            for (Cookie cookie : cookies){
                if (Contains.USERNAME_COOKIE.equals(cookie.getName())){
                    username = cookie.getValue();
                } else if (Contains.PASSWORD_COOKIE.equals(cookie.getName())){
                    password = cookie.getValue();
                }
            }
        }

        if (username != null && password != null){
            // 登录
            login(username, password, null, req, resp);
        } else{
            req.getRequestDispatcher("index.jsp").forward(req, resp);
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");

        String username = req.getParameter("username");
        String pass = req.getParameter("pass");
        String captcha = req.getParameter("captcha");

        String remember = req.getParameter("remember-me");

        // 校验验证码
        if (!CaptchaUtil.ver(captcha, req)) {
            req.setAttribute("msg", "验证码不正确");
            req.getRequestDispatcher("index.jsp").forward(req,resp);
            return;
        }


        login(username, pass, remember, req, resp);
    }

    public void login(String username, String pass, String remember, HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        AccountService service = new AccountService();

        Account account = service.login(username, pass);

        if (account == null){
            req.setAttribute("msg", "用户名或密码不正确");
            req.getRequestDispatcher("index.jsp").forward(req,resp);
        } else {
            // 登录成功后，将登录信息保存到 session
            req.getSession().setAttribute(Contains.LOGIN_FLAG, account);

            // 是否勾选了记住我
            if (remember != null){
                Cookie usernameCookie = new Cookie(Contains.USERNAME_COOKIE, account.getUsername());
                Cookie passwordCookie = new Cookie(Contains.PASSWORD_COOKIE, pass);

                // TODO( cookie 的时间 )

                resp.addCookie(usernameCookie);
                resp.addCookie(passwordCookie);
            }
            resp.sendRedirect("index");
        }
    }
}
